package com.doudanhua.basecode.jackson.configurer;

import com.doudanhua.basecode.jackson.time.DateTimeConverter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.TextNode;

import java.io.IOException;
import java.time.ZonedDateTime;

public class ZonedDateTimeModelDeserializer extends JsonDeserializer<ZonedDateTime> {
    private DateTimeConverter _dateConverter;

    public ZonedDateTimeModelDeserializer(DateTimeConverter dateConverter) {
        this._dateConverter = dateConverter;
    }

    @Override
    public ZonedDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException {
        ObjectCodec codec = jp.getCodec();
        TextNode node = codec.readTree(jp);
        String dateString = node.textValue();
        return _dateConverter.parseZonedDateTime(dateString);
    }
}
